﻿using Lonewolf.Entity;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Lonewolf.WebApp.Controllers
{
    /// <summary>
    /// 授权认证 需要权限控制的控制器继承它
    /// </summary>
    [Authorize]
    public class AuthController : Controller
    {
        protected async Task SignInAsync(CookieModel model)
        {
            if (model == null)
                return;
            var claims = new List<Claim>{
                    new Claim(ClaimTypes.Name, model.UserName),
                    new Claim("Id",model.Id),
                    new Claim(ClaimTypes.Role, "Administrator"),
                };
            var claimIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
            var claimsPrincipal = new ClaimsPrincipal(claimIdentity);
            // 在上面注册AddAuthentication时，指定了默认的Scheme，在这里便可以不再指定Scheme。
            await HttpContext.SignInAsync(claimsPrincipal);
            //获取数据
            //User.FindFirst("xxx").Value
        }
        /// <summary>
        /// 清空认证
        /// </summary>
        /// <returns></returns>
        protected async Task SignOutAsync()
        {
            await HttpContext.SignOutAsync();
        }
    }
}
